#include "multilinewidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDebug>
#include <QMessageBox>
#include "customtableview.h"
#include "taskprogresswidget.h"
#include "taskresultwidget.h"

multilinewidget::multilinewidget(QWidget *parent) : QWidget(parent)
{
    m_ptaskinfo = NULL;
    m_itemsselectedmap.clear();
    initControl();
    setLayoutWidget();
    showData();
}

multilinewidget::~multilinewidget()
{
    releseData();
}

void multilinewidget::initControl()
{
    m_tableview = new multilinetableview(this);
    QStringList header;
    //header << u8"序号" << u8"任务名称" << u8"任务类型" << u8"任务状态" << u8"任务进度" << u8"任务结果" << u8"任务完成";
    header << "序号" << "任务名称" << "任务类型" << "task State" << "任务进度" << "任务结果" << "任务完成";
    m_tableview->setHMultilineHeader(header);
    //合并单元格
    m_tableview->setUniteItems(1, 2, u8"test");
    m_tableview->setUniteItems(3, 3, u8"test");

    //设置字体特殊颜色
    m_tableview->SetItemTextColor(u8"test", QColor(30, 144, 255));
    m_tableview->SetItemTextColor(u8"test", QColor(255, 0, 0));
    m_tableview->SetItemTextColor(u8"test", QColor(50, 205, 50));
    m_tableview->SetItemTextColor(u8"test", QColor(255, 0, 0));
    m_tableview->SetItemTextColor(u8"test", QColor(255, 0, 0));
    m_tableview->SetItemTextColor(u8"test", QColor(255, 0, 0));
    m_tableview->SetItemTextColor(u8"test", QColor(0, 191, 255));


    connect(m_tableview->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
            this, SLOT(selectionChanged(QItemSelection, QItemSelection)));
    connect(m_tableview, SIGNAL(btnClicked(int, int)), this, SLOT(btnClicked(int, int)));
    connect(m_tableview, SIGNAL(headerSortClicked(int, int)), this, SLOT(headerSortClicked(int, int)));
}

void multilinewidget::setLayoutWidget()
{

}

void multilinewidget::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    m_tableview->setGeometry(0, 0, this->rect().width(), this->rect().height());
    int iwidth = rect().width() - 100;
    int icolumn = 7;
    for(int index = 0; index < icolumn; index++)
    {
        if(index == 0)
            m_tableview->setColumnWidth(0, 80);
        else
            m_tableview->setColumnWidth(index, iwidth / (icolumn - 1));
    }
}

void multilinewidget::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
}

void multilinewidget::showData(QString orderby, QString sorttype)
{
    //排序方式
    if(orderby.isEmpty())
        orderby = "taskid";
    //降序或升序
    if(sorttype.isEmpty())
        sorttype = "ASC";

    releseData();
    m_tableview->clear();
    int itotal = 0;
    QString str_sql =  "SELECT COUNT(*) AS count FROM task;";
    QSqlQuery query;
    query.setForwardOnly(true);
    bool success = query.exec(str_sql);
    if(!success)
    {
        QMessageBox::information(this, "test", "test");
        return;
    }
    int icount = 0;
    if(query.next())
    {
        icount = query.value(0).toInt();
    }
    if(icount <= 0)
    {
        return;
    }
    else
    {
        itotal = icount;
    }

    str_sql = "SELECT taskid,taskname,tasktype,showstatus,pixmaptype FROM task";

    if (!orderby.isEmpty() && !sorttype.isEmpty())
    {
        str_sql += " ORDER BY " + orderby + " " + sorttype;
    }

    m_ptaskinfo = new STRUCT_TASK_INFO[itotal];
    success = query.exec(str_sql);
    if(!success)
    {
        QMessageBox::information(this, u8"提示", u8"查询任务数据失败");
        return;
    }
    int index = 0;
    QList<int> pixmaptypelist;
    while(query.next())
    {
        m_ptaskinfo[index].taskid = query.value(0).toString();
        m_ptaskinfo[index].taskname = query.value(1).toString();
        m_ptaskinfo[index].tasktype = query.value(2).toString();
        m_ptaskinfo[index].taskshowstatus = query.value(3).toString();
        m_ptaskinfo[index].pixmaptype = query.value(4).toInt();
        QStringList rowlist;
        rowlist.append("");//第一列为序号，序号数自动生成，传入""即可
        rowlist.append(query.value(1).toString());
        rowlist.append(query.value(2).toString());
        rowlist.append(query.value(3).toString());
        rowlist.append(u8"任务进度");
        rowlist.append(u8"任务结果");
        //图片数据存入pixmaptypelist
        rowlist.append("");
        pixmaptypelist.append(m_ptaskinfo[index].pixmaptype);
        m_tableview->addRow(rowlist);
        m_tableview->setRowHeight(index, 25); //设置行高
        index++;
    }
    m_tableview->setpixmaptypelist(pixmaptypelist);
    setItemsSelected();
}

void multilinewidget::releseData()
{
    if (m_ptaskinfo != NULL)
    {
        delete[] m_ptaskinfo;
        m_ptaskinfo = NULL;
    }
}

void multilinewidget::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
    QModelIndexList selectrowslist = m_tableview->selectionModel()->selectedRows();
    m_itemsselectedmap.clear();
    for(int i = 0; i < selectrowslist.count(); i++)
    {
        m_itemsselectedmap.insert(m_ptaskinfo[selectrowslist[i].row()].taskid.toInt(), 1);
    }
}

void multilinewidget::btnClicked(int row, int column)
{
    //任务进度
    if(column == 4)
    {
        taskprogresswidget dlg;
        dlg.exec();
    }
    else if(column == 5)//任务结果
    {
        taskresultwidget dlg;
        dlg.exec();
    }
}

void multilinewidget::setItemsSelected()
{
    m_tableview->clearSelection();
    int totalcloumn = m_tableview->columnCount();
    int totalrow = m_tableview->rowCount();
    QItemSelection itemsselection;
    QModelIndexList selectedlist;
    for(int i = 0; i < totalrow; i++)
    {
        if(m_itemsselectedmap.find(m_ptaskinfo[i].taskid.toInt()) != m_itemsselectedmap.end())
        {
            QModelIndex leftindex = m_tableview->model()->index(i, 0);
            selectedlist.append(leftindex);
            QModelIndex rightindex = m_tableview->model()->index(i, totalcloumn - 1);
            QItemSelectionRange selectionrange(leftindex, rightindex);
            itemsselection.append(selectionrange);
        }
    }
    m_tableview->setIconSelection(selectedlist); //设置icon选中图片列表
    m_tableview->selectionModel()->select(itemsselection, QItemSelectionModel::Select); //设置行选中
}

void multilinewidget::headerSortClicked(int sort, int column)
{
    dataSort(sort, column);
}

void multilinewidget::dataSort(int sort, int column)
{
    QString orderby = "", sorttype = "";
    if (sort == 2)
        sorttype = "ASC";
    else
        sorttype = "DESC";

    switch(column)
    {
    case taskname:
        orderby = "taskname";
        break;
    case tasktype:
        orderby = "tasktype";
        break;
    case taskstatus:
        orderby = "showstatus";
        break;
    case pixmaptype:
        orderby = "pixmaptype";
        break;
    }
    if (orderby == "")
        return;
    showData(orderby, sorttype);
}





















